package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.CriterionExpandDao;
import cn.topevery.garden.entity.po.CriterionExpand;
import cn.topevery.garden.entity.query.CriterionExpandQuery;
import cn.topevery.garden.entity.vo.CriterionExpandVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 综合评价标准扩展表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class CriterionExpandDaoImpl extends BaseDaoImpl<CriterionExpand> implements CriterionExpandDao {

    @Transactional
    @Override
    public void insertAll(List<CriterionExpand> expandList, String criterionId, Integer userId) {
        SysUser user = UserRuntime.getSysUser();
        for (CriterionExpand criterionExpand : expandList) {
            criterionExpand.setCriterionId(criterionId);
            criterionExpand.setDbStatus(false);
            criterionExpand.setDbUpdateId(userId);
            criterionExpand.setDbUpdateDate(LocalDateTime.now());
            insert(user.userId, criterionExpand);
        }
    }

    @Override
    public List<CriterionExpand> getListByCriterionId(String criterionId) {
        String table = "t_criterion_expand tce";
        String columns = "tce.*,\n" +
                "            ( SELECT tsd.c_dict_name FROM t_sys_dict tsd WHERE tsd.c_ns_id = '15d4d3b2-40cf-454f-b0c3-a91389793398' AND tsd.c_dict_code = tce.c_level ) AS levelStr,\n" +
                "            ( SELECT tsd.c_dict_name FROM t_sys_dict tsd WHERE tsd.c_ns_id = 'eefa4ef3-b23b-4b91-aece-fc9bb38cdd95' AND tsd.c_dict_code = tce.c_operator ) AS operatorStr";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "tce.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(criterionId), "tce.c_criterion_id", ScriptConditionEnum.Eq, criterionId);
        return selectList(sqlQueryMate, CriterionExpand.class);
    }

    @Override
    public void deleteByCriterionId(String criterionId, Integer userId) {
        SqlUpdateMate updateMate = new SqlUpdateMate("t_criterion_expand tce");
        updateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(userId!=null, "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .where(true, "c_criterion_id", ScriptConditionEnum.Eq, criterionId);
        run(updateMate.getSql(), CommandType.Text, dbCommand -> {
            updateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public <P extends DbCsp> List<CriterionExpand> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<CriterionExpandVO> listPage(CriterionExpandQuery query) {
        String table = "t_criterion_expand tce";

        String columns = "tce.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tce.c_id desc");
        sqlQueryMate
                .where(true, "tce.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, CriterionExpandVO.class);
    }

    @Override
    public CriterionExpand getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }
}
